"""
题目：计算双链表中节点的总数，返回长度。
"""


class Node:
    def __init__(self, val=0, prev=None, next=None):
        self.val = val
        self.prev = prev
        self.next = next

def get_length(head):
    """计算双链表的长度"""
    length = 0
    current = head
    while current:
        length += 1
        current = current.next
    return length

def create_doubly_linked_list(arr):
    if not arr:
        return None
    head = Node(arr[0])
    current = head
    for val in arr[1:]:
        new_node = Node(val)
        current.next = new_node
        new_node.prev = current
        current = new_node
    return head

def traverse(head):
    result = []
    current = head
    while current:
        result.append(current.val)
        current = current.next
    return result

# 测试
if __name__ == "__main__":
    print(get_length(create_doubly_linked_list([])))  # 输出: 0
    print(get_length(create_doubly_linked_list([5])))  # 输出: 1
    print(get_length(create_doubly_linked_list([1, 2, 3, 4])))  # 输出: 4